package com.zxy.config;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : zhangxuyuan
 * @Project: springbootrabbitmq
 * @Package com.zxy.config
 * @date Date : 2021年04月20日 16:39
 */
@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue queueTTL()
    {
        Map<String, Object> props = new HashMap<>();
        // 对于该队列中的消息，设置都等待10s
        props.put("x-message-ttl", 20000);


        // 设置该队列所关联的死信交换器（当队列消息TTL到期后依然没有消费，则加 入死信队列）
        props.put("x-dead-letter-exchange", "dlxEx");
        // 设置该队列所关联的死信交换器的routingKey，如果没有特殊指定，使用原 队列的routingKey
        props.put("x-dead-letter-routing-key", "dlx");
        Queue queueTTL = new Queue("order",true,false,false,props);
        return queueTTL;
    }

    @Bean
    public Exchange exchangeTTL()
    {
        Exchange exchangeTTL = new DirectExchange("orderEx",true,false);
        return exchangeTTL;
    }

    @Bean
    public Binding bindingTTL(Exchange exchangeTTL,Queue queueTTL)
    {
        return BindingBuilder.bind(queueTTL).to(exchangeTTL).with("order").noargs();
    }


    @Bean
    public Queue queueDlx()
    {
        Queue queue=new Queue("dlx",true,false,false);
        return queue;
    }


    @Bean
    public Exchange exchangeDlx()
    {
        Exchange exchange=new DirectExchange("dlxEx",true,false);
        return exchange;
    }

    @Bean
    public Binding bindingDlx(Exchange exchangeDlx,Queue queueDlx)
    {
        return BindingBuilder.bind(queueDlx).to(exchangeDlx).with("dlx").noargs();
    }

}
